﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;

namespace TestDxfDocument
{
    class Driving_key
    {
        public static void _Driving_key(Dictionary<string, Object> data)
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中性线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层

            //基本参数-----------------------------------------------------------------------------------------------------
            double scale = 1; //放大系数
            double pi = Math.PI;
            string path = Convert.ToString(data["path"]);
            var type = data["type"].ToString()/*"C型"*/;

            //定义参数
            /*double b = 5 * scale;
            double h = 5 * scale;
            double L = 22 * scale;*/
            double L = Convert.ToDouble(data["l"]) * scale;
            double b = Convert.ToDouble(data["b"]) * scale;
            double h = Convert.ToDouble(data["h"]) * scale;

            //定义点-----------------------------------------------------------------------------------------------------------------------------------
            //第一部分------------------------------------------------------------------------------
            Vector2 v1 = new Vector2(0, 0);
            Vector2 v2 = new Vector2(v1.X + L * 1.2, v1.Y);
            Vector2 v1_1 = new Vector2(v1.X + fun.CalDistance(v1, v2) / 2, v1.Y + fun.CalDistance(v1, v2) / 2);
            Vector2 v2_1 = new Vector2(v1.X + fun.CalDistance(v1, v2) / 2, v1.Y - fun.CalDistance(v1, v2) / 2);
            Vector2 v3 = new Vector2(v1.X + (fun.CalDistance(v1, v2) - L) / 2, v1.Y + h * 0.3);
            Vector2 v4 = fun.Inters(v3, fun.Polar(v3, 45 * pi / 180, h), new Vector2(v3.X, h / 2), new Vector2(v3.X + L, h / 2));
            Vector2 v5 = fun.mirrorpoint(v4, v1_1, v2_1);
            Vector2 v6 = fun.mirrorpoint(v3, v1_1, v2_1);
            Vector2 v7 = fun.mirrorpoint(v6, v1, v2);
            Vector2 v8 = fun.mirrorpoint(v5, v1, v2);
            Vector2 v9 = fun.mirrorpoint(v4, v1, v2);
            Vector2 v10 = fun.mirrorpoint(v3, v1, v2);

            //第二部分------------------------------------------------------------------------------
            Vector2 v11 = new Vector2(v1.X, v1.Y - h * 3);
            Vector2 v12 = new Vector2(v11.X + L * 1.2, v11.Y);
            Vector2 v11_1 = new Vector2(v11.X + fun.CalDistance(v11, v12) / 2, v11.Y + fun.CalDistance(v11, v12) / 2);
            Vector2 v12_1 = new Vector2(v11.X + fun.CalDistance(v11, v12) / 2, v11.Y - fun.CalDistance(v11, v12) / 2);
            Vector2 v13 = new Vector2(v11.X + (fun.CalDistance(v1, v2) - L) / 2 + b / 2, v11.Y + b / 2);
            Vector2 v14 = fun.mirrorpoint(v13, v11_1, v12_1);
            Vector2 v15 = new Vector2(v13.X, v13.Y - b / 6);
            Vector2 v16 = fun.mirrorpoint(v15, v11_1, v12_1);
            Vector2 v17 = fun.mirrorpoint(v13, v11, v12);
            Vector2 v18 = fun.mirrorpoint(v14, v11, v12);
            Vector2 v19 = fun.mirrorpoint(v15, v11, v12);
            Vector2 v20 = fun.mirrorpoint(v16, v11, v12);
            Vector2 v21 = new Vector2(v11.X + (fun.CalDistance(v11, v12) - L) / 2, v11.Y + b / 2);
            Vector2 v22 = new Vector2(v21.X + L, v21.Y);
            Vector2 v23 = fun.mirrorpoint(v22, v11, v12);
            Vector2 v24 = fun.mirrorpoint(v21, v11, v12);
            Vector2 v25 = new Vector2(v21.X + b / 6, v21.Y - b / 6);
            Vector2 v26 = fun.mirrorpoint(v25, v11_1, v12_1);
            Vector2 v27 = fun.mirrorpoint(v26, v11, v12);
            Vector2 v28 = fun.mirrorpoint(v25, v11, v12);
            Vector2 v29 = v13;
            Vector2 v30 = v22;
            Vector2 v31 = v23;
            Vector2 v32 = v17;
            Vector2 v33 = v15;
            Vector2 v34 = v26;
            Vector2 v35 = v27;
            Vector2 v36 = v19;

            if (type.Equals("A型"))
            {
                //作图部分-------------------------------------------------------------------------------------------------
                //轮廓线
                LwPolyline polyline1 = new LwPolyline();
                polyline1.Vertexes.Add(new LwPolylineVertex(v3));
                polyline1.Vertexes.Add(new LwPolylineVertex(v4));
                polyline1.Vertexes.Add(new LwPolylineVertex(v5));
                polyline1.Vertexes.Add(new LwPolylineVertex(v6));
                polyline1.Vertexes.Add(new LwPolylineVertex(v7));
                polyline1.Vertexes.Add(new LwPolylineVertex(v8));
                polyline1.Vertexes.Add(new LwPolylineVertex(v9));
                polyline1.Vertexes.Add(new LwPolylineVertex(v10));
                polyline1.Vertexes.Add(new LwPolylineVertex(v3));
                fun.PolyFill(dxf, polyline1, 0.3, contour);

                LwPolyline polyline2 = new LwPolyline();
                polyline2.Vertexes.Add(new LwPolylineVertex(v3));
                polyline2.Vertexes.Add(new LwPolylineVertex(v6));
                fun.PolyFill(dxf, polyline2, 0.3, contour);

                LwPolyline polyline3 = new LwPolyline();
                polyline3.Vertexes.Add(new LwPolylineVertex(v10));
                polyline3.Vertexes.Add(new LwPolylineVertex(v7));
                fun.PolyFill(dxf, polyline3, 0.3, contour);

                LwPolyline polyline4 = new LwPolyline();
                polyline4.Vertexes.Add(new LwPolylineVertex(v13));
                polyline4.Vertexes.Add(new LwPolylineVertex(v14));//0
                polyline4.Vertexes.Add(new LwPolylineVertex(v18));//1
                polyline4.Vertexes.Add(new LwPolylineVertex(v17));//2
                polyline4.Vertexes.Add(new LwPolylineVertex(v13));//3
                polyline4.Vertexes[1].Bulge = -Math.Tan((180) * pi / 180 / 4);
                polyline4.Vertexes[3].Bulge = -Math.Tan((180) * pi / 180 / 4);
                fun.PolyFill(dxf, polyline4, 0.3, contour);

                LwPolyline polyline5 = new LwPolyline();
                polyline5.Vertexes.Add(new LwPolylineVertex(v15));
                polyline5.Vertexes.Add(new LwPolylineVertex(v16));//0
                polyline5.Vertexes.Add(new LwPolylineVertex(v20));//1
                polyline5.Vertexes.Add(new LwPolylineVertex(v19));//2
                polyline5.Vertexes.Add(new LwPolylineVertex(v15));//3
                polyline5.Vertexes[1].Bulge = -Math.Tan((180) * pi / 180 / 4);
                polyline5.Vertexes[3].Bulge = -Math.Tan((180) * pi / 180 / 4);
                fun.PolyFill(dxf, polyline5, 0.3, contour);

                //标注------------------------------------------------------------------------------------------
                //定义
                double height = 1;
                double arrowsize = 1;
                double exline = 1;

                //第一部分---------------------------------------------------
                //直线标注
                //左侧
                fun.dimrot1(dxf, v4, v9, 90, L / 4, 1, height, arrowsize, exline, "h=" + Math.Round(h, 1), "", "");
                //下侧
                fun.dimrot1(dxf, v10, v7, 180, h, 1, height, arrowsize, exline, "L=" + Math.Round(L, 1), "", "");

                //第二部分---------------------------------------------------
                //直线标注
                //左侧
                fun.dimrot1(dxf, v13, v17, 90, L / 4, 1, height, arrowsize, exline, "b=" + Math.Round(b, 1), "", "");

                //箭头标注
                fun.jt1(dxf, v16, -135, b / 3, 1, "b/2", height);
            }

            if (type.Equals("B型"))
            {
                //作图部分-------------------------------------------------------------------------------------------------
                //轮廓线
                LwPolyline polyline1 = new LwPolyline();
                polyline1.Vertexes.Add(new LwPolylineVertex(v3));
                polyline1.Vertexes.Add(new LwPolylineVertex(v4));
                polyline1.Vertexes.Add(new LwPolylineVertex(v5));
                polyline1.Vertexes.Add(new LwPolylineVertex(v6));
                polyline1.Vertexes.Add(new LwPolylineVertex(v7));
                polyline1.Vertexes.Add(new LwPolylineVertex(v8));
                polyline1.Vertexes.Add(new LwPolylineVertex(v9));
                polyline1.Vertexes.Add(new LwPolylineVertex(v10));
                polyline1.Vertexes.Add(new LwPolylineVertex(v3));
                fun.PolyFill(dxf, polyline1, 0.3, contour);

                LwPolyline polyline2 = new LwPolyline();
                polyline2.Vertexes.Add(new LwPolylineVertex(v3));
                polyline2.Vertexes.Add(new LwPolylineVertex(v6));
                fun.PolyFill(dxf, polyline2, 0.3, contour);

                LwPolyline polyline3 = new LwPolyline();
                polyline3.Vertexes.Add(new LwPolylineVertex(v10));
                polyline3.Vertexes.Add(new LwPolylineVertex(v7));
                fun.PolyFill(dxf, polyline3, 0.3, contour);

                LwPolyline polyline4 = new LwPolyline();
                polyline4.Vertexes.Add(new LwPolylineVertex(v21));
                polyline4.Vertexes.Add(new LwPolylineVertex(v22));//0
                polyline4.Vertexes.Add(new LwPolylineVertex(v23));//1
                polyline4.Vertexes.Add(new LwPolylineVertex(v24));//2
                polyline4.Vertexes.Add(new LwPolylineVertex(v21));//3
                fun.PolyFill(dxf, polyline4, 0.3, contour);

                LwPolyline polyline5 = new LwPolyline();
                polyline5.Vertexes.Add(new LwPolylineVertex(v25));
                polyline5.Vertexes.Add(new LwPolylineVertex(v26));
                polyline5.Vertexes.Add(new LwPolylineVertex(v27));
                polyline5.Vertexes.Add(new LwPolylineVertex(v28));
                polyline5.Vertexes.Add(new LwPolylineVertex(v25));
                fun.PolyFill(dxf, polyline5, 0.3, contour);

                LwPolyline polyline6 = new LwPolyline();
                polyline6.Vertexes.Add(new LwPolylineVertex(v21));
                polyline6.Vertexes.Add(new LwPolylineVertex(v25));
                fun.PolyFill(dxf, polyline6, 0.3, contour);

                LwPolyline polyline7 = new LwPolyline();
                polyline7.Vertexes.Add(new LwPolylineVertex(v22));
                polyline7.Vertexes.Add(new LwPolylineVertex(v26));
                fun.PolyFill(dxf, polyline7, 0.3, contour);

                LwPolyline polyline8 = new LwPolyline();
                polyline8.Vertexes.Add(new LwPolylineVertex(v23));
                polyline8.Vertexes.Add(new LwPolylineVertex(v27));
                fun.PolyFill(dxf, polyline8, 0.3, contour);

                LwPolyline polyline9 = new LwPolyline();
                polyline9.Vertexes.Add(new LwPolylineVertex(v24));
                polyline9.Vertexes.Add(new LwPolylineVertex(v28));
                fun.PolyFill(dxf, polyline9, 0.3, contour);

                //标注------------------------------------------------------------------------------------------
                //定义
                double height = 1;
                double arrowsize = 1;
                double exline = 1;

                //第一部分---------------------------------------------------
                //直线标注
                //左侧
                fun.dimrot1(dxf, v4, v9, 90, L / 4, 1, height, arrowsize, exline, "h=" + Math.Round(h, 1), "", "");
                //下侧
                fun.dimrot1(dxf, v10, v7, 180, h, 1, height, arrowsize, exline, "L=" + Math.Round(L, 1), "", "");

                //第二部分---------------------------------------------------
                //直线标注
                //左侧
                fun.dimrot1(dxf, v13, v17, 90, L / 4, 1, height, arrowsize, exline, "b=" + Math.Round(b, 1), "", "");

                //箭头标注
                fun.jt1(dxf, v21, 45, b / 3, 1, "y", height);
                fun.jt1(dxf, v22, 45, b / 3, 1, "y", height);
                fun.jt1(dxf, v24, -135, b / 3, 1, "y", height);
                fun.jt1(dxf, v23, -135, b / 3, 1, "y", height);
            }

            if (type.Equals("C型"))
            {
                //作图部分-------------------------------------------------------------------------------------------------
                //轮廓线
                LwPolyline polyline1 = new LwPolyline();
                polyline1.Vertexes.Add(new LwPolylineVertex(v3));
                polyline1.Vertexes.Add(new LwPolylineVertex(v4));
                polyline1.Vertexes.Add(new LwPolylineVertex(v5));
                polyline1.Vertexes.Add(new LwPolylineVertex(v6));
                polyline1.Vertexes.Add(new LwPolylineVertex(v7));
                polyline1.Vertexes.Add(new LwPolylineVertex(v8));
                polyline1.Vertexes.Add(new LwPolylineVertex(v9));
                polyline1.Vertexes.Add(new LwPolylineVertex(v10));
                polyline1.Vertexes.Add(new LwPolylineVertex(v3));
                fun.PolyFill(dxf, polyline1, 0.3, contour);

                LwPolyline polyline2 = new LwPolyline();
                polyline2.Vertexes.Add(new LwPolylineVertex(v3));
                polyline2.Vertexes.Add(new LwPolylineVertex(v6));
                fun.PolyFill(dxf, polyline2, 0.3, contour);

                LwPolyline polyline3 = new LwPolyline();
                polyline3.Vertexes.Add(new LwPolylineVertex(v10));
                polyline3.Vertexes.Add(new LwPolylineVertex(v7));
                fun.PolyFill(dxf, polyline3, 0.3, contour);

                LwPolyline polyline4 = new LwPolyline();
                polyline4.Vertexes.Add(new LwPolylineVertex(v29));
                polyline4.Vertexes.Add(new LwPolylineVertex(v30));//0
                polyline4.Vertexes.Add(new LwPolylineVertex(v31));//1
                polyline4.Vertexes.Add(new LwPolylineVertex(v32));//2
                polyline4.Vertexes.Add(new LwPolylineVertex(v29));//3
                polyline4.Vertexes[3].Bulge = -Math.Tan((180) * pi / 180 / 4);
                fun.PolyFill(dxf, polyline4, 0.3, contour);

                LwPolyline polyline5 = new LwPolyline();
                polyline5.Vertexes.Add(new LwPolylineVertex(v33));
                polyline5.Vertexes.Add(new LwPolylineVertex(v34));//0
                polyline5.Vertexes.Add(new LwPolylineVertex(v35));//1
                polyline5.Vertexes.Add(new LwPolylineVertex(v36));//2
                polyline5.Vertexes.Add(new LwPolylineVertex(v33));//3
                polyline5.Vertexes[3].Bulge = -Math.Tan((180) * pi / 180 / 4);
                fun.PolyFill(dxf, polyline5, 0.3, contour);

                LwPolyline polyline6 = new LwPolyline();
                polyline6.Vertexes.Add(new LwPolylineVertex(v30));
                polyline6.Vertexes.Add(new LwPolylineVertex(v34));
                fun.PolyFill(dxf, polyline6, 0.3, contour);

                LwPolyline polyline7 = new LwPolyline();
                polyline7.Vertexes.Add(new LwPolylineVertex(v31));
                polyline7.Vertexes.Add(new LwPolylineVertex(v35));
                fun.PolyFill(dxf, polyline7, 0.3, contour);

                //标注------------------------------------------------------------------------------------------
                //定义
                double height = 1;
                double arrowsize = 1;
                double exline = 1;

                //第一部分---------------------------------------------------
                //直线标注
                //左侧
                fun.dimrot1(dxf, v4, v9, 90, L / 4, 1, height, arrowsize, exline, "h=" + Math.Round(h, 1), "", "");
                //下侧
                fun.dimrot1(dxf, v10, v7, 180, h, 1, height, arrowsize, exline, "L=" + Math.Round(L, 1), "", "");

                //第二部分---------------------------------------------------
                //直线标注
                //左侧
                fun.dimrot1(dxf, v13, v17, 90, L / 4, 1, height, arrowsize, exline, "b=" + Math.Round(b, 1), "", "");

                //箭头标注
                fun.jt1(dxf, v30, 45, b / 3, 1, "y", height);
                fun.jt1(dxf, v31, -45, b / 3, 1, "y", height);
                fun.jt1(dxf, v33, -45, b / 3, 1, "b/2", height);
            }

            //定义格式，根据路径导出
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);
        }
    }
}
